package model

import (
	"context"
	"fmt"
	"go.mongodb.org/mongo-driver/bson"
)

type DeviceModel struct {
	ProductName string  ` bson:"product_name"`
	DeviceName string   `bson:"device_name"`
	BrokerUsername string ` bson:"broker_username"`
	Secret string   ` bson:"secret"`
	Status string ` bson:"status"`

}

func NewDeviceModel(productName,deviceName,secret string) *DeviceModel {
	return &DeviceModel{
		ProductName:productName,
		DeviceName:deviceName,
		Secret:secret,
		BrokerUsername:productName+"/"+deviceName,
		Status:"active",
	}
}

func (d *DeviceModel) Insert() error{
	doc,_:=bson.Marshal(d)

	db:=MonClient.Database("iothub")
	table := db.Collection("devices")
	ctx := context.Background()
	res,err :=table.InsertOne(ctx,doc)
	fmt.Println(res.InsertedID)
	return err
}

func (d *DeviceModel)Disconnect()  {

}

func (d *DeviceModel)Connected()  {

}